home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / filename_absolute.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-13  |  2.6 KB  |  97 lines

  1. //
  2. // "$Id: filename_absolute.cxx,v 1.5 1999/01/13 16:56:05 mike Exp $"
  3. //
  4. // Filename expansion routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. /* expand a file name by prepending current directory, deleting . and
  27.    .. (not really correct for symbolic links) between the prepended
  28.    current directory.  Use $PWD if it exists.
  29.    Returns true if any changes were made.
  30. */
  31.  
  32. #include <FL/filename.H>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #if defined(WIN32) && !defined(CYGNUS)
  36. # include <direct.h>
  37. //# define getcwd(a,b) _getdcwd(0,a,b)
  38. #else
  39. # include <unistd.h>
  40. # ifdef __EMX__
  41. #  define getcwd _getcwd2
  42. # endif
  43. #endif
  44.  
  45. #if defined(WIN32) || defined(__EMX__)
  46. inline int isdirsep(char c) {return c=='/' || c=='\\';}
  47. #else
  48. #define isdirsep(c) ((c)=='/')
  49. #endif
  50.  
  51. int filename_absolute(char *to,const char *from) {
  52.  
  53.   if (isdirsep(*from) || *from == '|'
  54. #if defined(WIN32) || defined(__EMX__)
  55.       || from[1]==':'
  56. #endif
  57.       ) {
  58.     strcpy(to,from);
  59.     return 0;
  60.   }
  61.  
  62.   char *a,temp[FL_PATH_MAX];
  63.   const char *start = from;
  64.  
  65.   a = getenv("PWD");
  66.   if (a) strncpy(temp,a,FL_PATH_MAX);
  67.   else {a = getcwd(temp,FL_PATH_MAX); if (!a) return 0;}
  68. #if defined(WIN32) || defined(__EMX__)
  69.   for (a = temp; *a; a++) if (*a=='\\') *a = '/'; // ha ha
  70. #else
  71.   a = temp+strlen(temp);
  72. #endif
  73.   if (isdirsep(*(a-1))) a--;
  74.   /* remove intermediate . and .. names: */
  75.   while (*start == '.') {
  76.     if (start[1]=='.' && isdirsep(start[2])) {
  77.       char *b;
  78.       for (b = a-1; b >= temp && !isdirsep(*b); b--);
  79.       if (b < temp) break;
  80.       a = b;
  81.       start += 3;
  82.     } else if (isdirsep(start[1])) {
  83.       start += 2;
  84.     } else
  85.       break;
  86.   }
  87.   *a++ = '/';
  88.   strcpy(a,start);
  89.   strcpy(to,temp);
  90.   return 1;
  91.  
  92. }
  93.  
  94. //
  95. // End of "$Id: filename_absolute.cxx,v 1.5 1999/01/13 16:56:05 mike Exp $".
  96. //
  97.